package com.baimawangzi.recovery.servlet.activity;

import com.baimawangzi.recovery.bean.Activity;
import com.baimawangzi.recovery.service.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet("/activityupdate.do")
public class ActivityUpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ActivityServiceImpl activityService = new ActivityServiceImpl();
        //解析url参数
        String sid=req.getParameter("id");
        String title=req.getParameter("title");
        String content=req.getParameter("content");
        String sstarttime=req.getParameter("starttime");
        String stat=req.getParameter("stat");
        //时间转换
        java.util.Date starttime=null;

        try {
            starttime = new SimpleDateFormat("yyyy-MM-dd").parse(sstarttime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //new java.sql.Date(birthday.getTime());//重点
        Activity activity = new Activity(Integer.parseInt(sid), title, content, new Date(starttime.getTime()), stat);

        int a = activityService.update(activity);
        if(a>0){//返回的信息
            req.setAttribute("msg","修改活动成功");
        }
        else {
            req.setAttribute("msg","修改活动失败");
        }
        try {
            req.getRequestDispatcher("activitylist.do").forward(req,resp);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
